//
//  ResultViewController.m
//  MobileStudy
//
//  Created by cuihongbao on 13-6-28.
//  Copyright (c) 2013年 hongbao.cui. All rights reserved.
//

#import "ResultViewController.h"
#import "ApplicationUtils.h"
#import "FMNavgationItem.h"
#import "ResultLookViewController.h"
#import "Sqlite.h"
@interface ResultViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong) NSMutableArray   *dataArray;
@end

@implementation ResultViewController
@synthesize dataDict;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)backBtnClicked:(id)sender{
    [self.navigationController dismissViewControllerAnimated:YES completion:^{
    }];
    if (self.resultBackBlock) {
        self.resultBackBlock();
    }
}
-(IBAction)answerResult:(id)sender{
    
    FMSCHBLog(@"%@",dataDict);
    NSMutableDictionary *newDict = [NSMutableDictionary dictionaryWithCapacity:1];
    NSString *papaer_id = [dataDict objectForKey:@"paper_id"];
    NSUserDefaults *standard = [NSUserDefaults standardUserDefaults];
    NSString *paperIDWithName =[NSString stringWithFormat:@"%@_%@",papaer_id,@"userName"];
    [standard setObject:@"0" forKey:paperIDWithName];//判断是否做过试题
    [newDict setObject:papaer_id forKey:@"paperID"];
    [newDict setObject:[dataDict objectForKey:@"paper_title"] forKey:@"name"];
    [newDict setObject:[dataDict objectForKey:@"exam_time"] forKey:@"exam_time"];
    ResultLookViewController *synExercisesViewController = [[ResultLookViewController alloc] initWithNibName:@"ResultLookViewController" bundle:nil];
    synExercisesViewController.tableType = HBListTableViewTypeResultLook;
    synExercisesViewController.inforDict = [NSDictionary dictionaryWithDictionary:newDict];
    __weak ResultViewController *weakSelf = self;
    synExercisesViewController.lookBackBlock = ^(){
        if (weakSelf.resultBackBlock) {
            weakSelf.resultBackBlock();
        }
    };
    [self.navigationController pushViewController:synExercisesViewController animated:YES];
}
-(void)updateMainView{
    if (dataDict) {
        if (EDUCATION) {
            NSString *paper_score = [dataDict objectForKey:@"paper_score"];
            if ([paper_score isEqualToString:@""]||!paper_score||[paper_score isEqualToString:@"0"]) {
                paper_score = @"-";
            }
            [scroeLabel setText:paper_score];
            [getLabel setText:[dataDict objectForKey:@"exam_score"]];
        }else{
//            [scroeNameLabel setHidden:YES];
//            [scroeLabel setHidden:YES];
//            [getLabel setHidden:YES];
//            [getNameLabel setHidden:YES];
            
//            CGRect rect0 = timeIconImageView.frame;
//            rect0.origin.x = scroeNameLabel.frame.origin.x+5;
//            rect0.origin.y = scroeNameLabel.frame.origin.y+3;
//            [timeIconImageView setFrame:rect0];
//            
//            CGRect rect = timeLabel.frame;
//            rect.origin = scroeLabel.frame.origin;
//            [timeLabel setFrame:rect];
            NSString *paper_score = [dataDict objectForKey:@"paper_score"];
            if ([paper_score isEqualToString:@""]||!paper_score||[paper_score isEqualToString:@"0"]) {
                paper_score = @"-";
            }
            [scroeLabel setText:paper_score];
            [getLabel setText:[dataDict objectForKey:@"exam_score"]];
        }
//        int seconds = [[dataDict objectForKey:@"exam_use_time"] integerValue];
//        NSString *time = [NSString stringWithFormat:@"%02i:%02i",seconds/60,seconds%60];
        NSString *time =  [dataDict objectForKey:@"exam_use_time"];
        [timeLabel setText:time];
        int rightCount = [[dataDict objectForKey:@"sum"] intValue];
        int allCount = [[dataDict objectForKey:@"qst_count"] intValue];
        int errorCount = allCount - rightCount;
        
        [rightLabel setText:[dataDict objectForKey:@"sum"]];
        [errorLabel setText:[NSString stringWithFormat:@"%d",errorCount]];
        float right = (float)rightCount/allCount;
        
        NSString *contentStr = nil;

        if (right>0.8) {
            contentStr = @"不错哦,继续努力！";
            [discreptionLabel setText:contentStr];
            [bqImageView setImage:[ApplicationUtils getLocalImage:@"m_card_anser009"]];
        }else{
            contentStr = @"很遗憾你没有通过本次测试，继续努力，加油！";
            [discreptionLabel setText:contentStr];
            [bqImageView setImage:[ApplicationUtils getLocalImage:@"m_card_anser001"]];
            [anyliseLabel setText:[dataDict objectForKey:@"paper_title"]];
        }
        CGSize sizeName = [contentStr
                           sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSizeMake(FMSCREENWIDTH-190,MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
        [discreptionLabel setFont:[UIFont systemFontOfSize:17.0]];
        [discreptionLabel setLineBreakMode:NSLineBreakByWordWrapping];
        [discreptionLabel setNumberOfLines:0];
        [discreptionLabel setFrame:CGRectMake(116, 62, FMSCREENWIDTH-190, sizeName.height)];
        [rightProgressView setProgress:right];
        [errorProgressView setProgress:1-right];
    }
}
-(void)initRightBar{
//    self.view.backgroundColor = [UIColor colorWithRed:(float)0xF5/0xFF green:(float)0xF4/0xFF blue:(float)0xD4/0xFF alpha:1.0f];
    self.view.backgroundColor = [UIColor whiteColor];
    FMNavgationItem* leftItem = [[FMNavgationItem alloc] init];
    [leftItem.backButton addTarget:self action:@selector(backBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItems = [leftItem getNavgationItemWithType:WRNavgationTypeLeft Ima:[ApplicationUtils getLocalImage:@"Navigation-Bar_Return_btn_@2x"] ImaHighlight:[ApplicationUtils getLocalImage:@"Navigation-Bar_Return_btn_@2x"] title:@""];
}
- (void)viewDidLoad
{
//    float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
//    if (systemVersion >= 7.0)
//    {
//        //某个仅支持7.0以上版本的方法
//        self.edgesForExtendedLayout = UIRectEdgeNone;
//        
//    }
//    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    [super viewDidLoad];
    self.navigationItem.title = @"测试结果";
    [self initRightBar];
    [self updateMainView];
    UIImage *btnImage = [[ApplicationUtils getLocalImage:@"m_card_picture002"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)];
//    [answerResultBtn setFrame:CGRectMake(0, FMSCREENHEIGHT-44-20-34, FMSCREENWIDTH, 34)];
    [answerResultBtn setBackgroundImage:btnImage forState:UIControlStateNormal];
    [answerResultBtn setBackgroundImage:btnImage forState:UIControlStateHighlighted];
    UIImage *bgImage = [[ApplicationUtils getLocalImage:@"content_bg@2x"] resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)];
    [bgImageView setImage:bgImage];
//    [bgImageView setFrame:CGRectMake(0, 0, FMSCREENWIDTH, FMSCREENHEIGHT-20-44)];
//    [super viewDidLoad];
    NSString *paper_id = [dataDict objectForKey:@"paper_id"];
    _dataArray = [Sqlite selectUserAnswerErrorTestQuestion:paper_id];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    if (IS_STUDY) {
//        
//    }else{
//        self.navigationController.navigationBarHidden = NO;
//    }
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    if (IS_STUDY) {
//        
//    }else{
//        self.navigationController.navigationBarHidden = YES;
//    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc{
    if (bqImageView) {
        bqImageView = nil;
    }
    if (discreptionLabel) {
        discreptionLabel = nil;
    }
    if (scroeLabel) {
        scroeLabel = nil;
    }
    if (getLabel) {
        getLabel =  nil;
    }
    if (timeLabel) {
        timeLabel = nil;
    }
    if (rightProgressView) {
        rightProgressView = nil;
    }
    if (rightLabel) {
        rightLabel  = nil;
    }
    if (errorProgressView) {
        errorProgressView = nil;
    }
    if (errorLabel) {
        errorLabel = nil;
    }
    if (anyliseLabel) {
       anyliseLabel = nil;
    }
    if (dataDict) {
        dataDict = nil;
    }
    answerResultBtn= nil;
    bgImageView = nil;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([self.dataArray count]>0) {
        return [self.dataArray count];
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString  *identify = @"MyCell";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identify];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.contentView.backgroundColor = [UIColor whiteColor];
    }
    NSString *catory_Name = [[self.dataArray objectAtIndex:indexPath.row] objectForKey:@"category_name"];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld.%@",(long)indexPath.row+1,catory_Name];
    cell.textLabel.font = [UIFont systemFontOfSize:14.0];
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.textLabel.backgroundColor = [UIColor clearColor];
    return cell;
}
-(CGFloat)heightWithString:(NSString *)category_name{
    CGSize size = [category_name sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(hbtableView.frame.size.width, 4000) lineBreakMode:NSLineBreakByWordWrapping];
    return size.height;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *catory_Name = [[self.dataArray objectAtIndex:indexPath.row] objectForKey:@"category_name"];
    CGFloat height = [self heightWithString:catory_Name];
    return MAX(height, 45);
}
@end
